package com.xopencode;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.*;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * @Auther: cilu
 * @Date: 2022/3/18 14:46
 * @Description:
 */
public class MyCommentGenerator extends DefaultCommentGenerator {

    private Properties properties;
    private Properties systemPro;

    private boolean suppressDate;
    private boolean suppressAllComments;
    private String currentDateStr;

    public MyCommentGenerator() {
        super();
        properties = new Properties();
        systemPro = System.getProperties();
        suppressDate = false;
        suppressAllComments = false;
        currentDateStr = (new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")).format(new Date());
    }

    /**
	 *  实体参数自定义注解
	 *
	 * */
	@Override
	public void addFieldComment(Field field, IntrospectedTable introspectedTable,
                                IntrospectedColumn introspectedColumn) {
		if (suppressAllComments) {
			return;
		}
		StringBuilder sb = new StringBuilder();
		field.addJavaDocLine("/**");
		sb.append(" * <pre><b>column</b> : ");
		sb.append(introspectedColumn.getActualColumnName());
		sb.append("\n");
		sb.append("     * <br><b>comment</b> : " + introspectedColumn.getRemarks());
		sb.append("\n");
		sb.append("     * <br><b>type</b> : " + introspectedColumn.getJdbcTypeName() + "( "
				+ introspectedColumn.getLength() + " )");
		sb.append("\n");
		sb.append("         * 允许空: ( " + introspectedColumn.isNullable() + " ) 缺省值: ( "
				+ introspectedColumn.getDefaultValue() + " )");
		sb.append("\n");
		sb.append("     *");
		sb.append("\n");
		sb.append("     * @version " + currentDateStr);
		field.addJavaDocLine(sb.toString());
		field.addJavaDocLine(" */");
	}

	@Override
	public void addFieldComment(Field field, IntrospectedTable introspectedTable) {

	}

	/**
	 *
	 * mapper接口自定义注解
	 * */
	@Override
	public void addGeneralMethodComment(Method method, IntrospectedTable introspectedTable) {
		if (suppressAllComments) {
			return;
		}
		StringBuilder sb = new StringBuilder();
		sb.append("/**");
		sb.append("\n");
		sb.append("	* ");
		sb.append("\n");
		sb.append("	* @author {自己名称}" + "\n");
		if (!suppressDate) {
			sb.append("	* @date " + currentDateStr + "\n");
		}

		List<Parameter> parameters = method.getParameters();

		for (Parameter parameter : parameters) {
			sb.append("	* @param " + parameter.getName() + "\n");
		}
		sb.append("	* @return " + method.getReturnType());
		sb.append("\n" + "	*/");
		method.addJavaDocLine(sb.toString());
	}

	// Get 方法注釋
	@Override
	public void addGetterComment(Method method, IntrospectedTable introspectedTable,
			IntrospectedColumn introspectedColumn) {
		if (suppressAllComments) {
			if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
				method.addJavaDocLine("//获取:" + introspectedColumn.getRemarks());
			}
		} else {
			super.addGetterComment(method, introspectedTable, introspectedColumn);
		}
	}

	// Set 方法注釋
	@Override
	public void addSetterComment(Method method, IntrospectedTable introspectedTable,
			IntrospectedColumn introspectedColumn) {
		if (suppressAllComments) {
			if (StringUtility.stringHasValue(introspectedColumn.getRemarks())) {
				method.addJavaDocLine("//设置:" + introspectedColumn.getRemarks());
			}
		} else {
			super.addSetterComment(method, introspectedTable, introspectedColumn);
		}
	}

    @Override
	public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable, boolean markAsDoNotDelete) {
		if (suppressAllComments) {
			return;
		}

		StringBuilder sb = new StringBuilder();

		innerClass.addJavaDocLine("/**");
		sb.append(" * ");
		sb.append(introspectedTable.getFullyQualifiedTable());
		innerClass.addJavaDocLine(sb.toString());

		sb.setLength(0);
		sb.append(" * @author ");
		sb.append(systemPro.getProperty("user.name"));
		sb.append(" ");
		sb.append(currentDateStr);

		addJavadocTag(innerClass, markAsDoNotDelete);

		innerClass.addJavaDocLine(" */");
	}

    @Override
	public void addClassComment(InnerClass innerClass, IntrospectedTable introspectedTable) {
		if (suppressAllComments) {
			return;
		}
		StringBuilder sb = new StringBuilder();
		innerClass.addJavaDocLine("/**");
		sb.append(" * ");
		sb.append(introspectedTable.getFullyQualifiedTable());
		sb.append(" ");
		sb.append(getDateString());
		innerClass.addJavaDocLine(sb.toString());
		innerClass.addJavaDocLine(" */");
	}

	/**
	 * 生成实体类头自定义注释
	 * */
	@Override
	public void addModelClassComment(TopLevelClass topLevelClass, IntrospectedTable introspectedTable) {
		if (suppressAllComments) {
			return;
		}
		StringBuilder sb = new StringBuilder();
		sb.append("/**");
		sb.append("\n");
		sb.append(" * <p> TableName : " + introspectedTable.getFullyQualifiedTableNameAtRuntime() + " </p >");
		sb.append("\n");
		sb.append(" * @author {公司名称/自己名称}" + "\n");
		sb.append(" * CopyRight Copyright 1999-2019 {公司官网} Inc. All rights reserved. \n");
		if (!suppressDate) {
			sb.append(" * @date " + currentDateStr + "\n");
		}
		sb.append(" */");
		topLevelClass.addJavaDocLine(sb.toString());
	}


}
